package com.axin.oss.service.impl;

import com.axin.common.core.domain.web.TableDataInfo;
import com.axin.common.core.exception.ServiceException;
import com.axin.common.mybatis.core.ServicePlusImpl;
import com.axin.common.mybatis.page.PagePlus;
import com.axin.common.mybatis.utils.PageUtils;
import com.axin.oss.api.domain.SysOss;
import com.axin.oss.api.dto.SysOssDTO;
import com.axin.oss.api.vo.SysOssVo;
import com.axin.oss.api.vo.UploadResult;
import com.axin.oss.factory.OssFactory;
import com.axin.oss.mapper.SysOssMapper;
import com.axin.oss.service.ICloudStorageStrategy;
import com.axin.oss.service.ISysOssConfigService;
import com.axin.oss.service.ISysOssService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 对象存储配置Service业务层处理
 *
 * @Author axin
 * @Date 2021/11/3 22:22
 **/
@Service
public class SysOssServiceImpl extends ServicePlusImpl<SysOssMapper, SysOss, SysOssVo> implements ISysOssService {

    /**
     * 分页查询
     * @param dto
     * @return
     */
    @Override
    public TableDataInfo<SysOssVo> queryPageList(SysOssDTO dto) {

        PagePlus<SysOss, SysOssVo> result = pageVo(PageUtils.buildPagePlus(), buildQueryWrapper(dto));
        return PageUtils.buildDataInfo(result);
    }


    private LambdaQueryWrapper<SysOss> buildQueryWrapper(SysOssDTO dto) {
        Map<String, Object> params = dto.getParams();
        LambdaQueryWrapper<SysOss> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(dto.getFileName()), SysOss::getFileName, dto.getFileName());
        lqw.like(StringUtils.isNotBlank(dto.getOriginalName()), SysOss::getOriginalName, dto.getOriginalName());
        lqw.eq(StringUtils.isNotBlank(dto.getFileSuffix()), SysOss::getFileSuffix, dto.getFileSuffix());
        lqw.eq(StringUtils.isNotBlank(dto.getUrl()), SysOss::getUrl, dto.getUrl());
        lqw.between(params.get("beginCreateTime") != null && params.get("endCreateTime") != null,
                SysOss::getCreateTime, params.get("beginCreateTime"), params.get("endCreateTime"));
        lqw.eq(StringUtils.isNotBlank(dto.getCreateBy()), SysOss::getCreateBy, dto.getCreateBy());
        lqw.eq(StringUtils.isNotBlank(dto.getService()), SysOss::getService, dto.getService());
        return lqw;
    }

    /**
     * 文件上传
     * @param file
     * @return
     */
    @Override
    public SysOss upload(MultipartFile file) {
        String originalfileName = file.getOriginalFilename();
        String suffix = StringUtils.substring(originalfileName, originalfileName.lastIndexOf("."), originalfileName.length());
        ICloudStorageStrategy storage = OssFactory.instance();
        UploadResult uploadResult;
        try {
            uploadResult = storage.uploadSuffix(file.getBytes(), suffix, file.getContentType());
        } catch (IOException e) {
            throw new ServiceException(e.getMessage());
        }
        // 保存文件信息
        SysOss oss = new SysOss()
                .setUrl(uploadResult.getUrl())
                .setFileSuffix(suffix)
                .setFileName(uploadResult.getFilename())
                .setOriginalName(originalfileName)
                .setService(storage.getServiceType());
        save(oss);
        return oss;
    }

    /**
     * 文件删除
     * @param ids
     * @param isValid
     * @return
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            // 做一些业务上的校验,判断是否需要校验
        }
        List<SysOss> list = listByIds(ids);
        for (SysOss sysOss : list) {
            ICloudStorageStrategy storage = OssFactory.instance(sysOss.getService());
            storage.delete(sysOss.getUrl());
        }
        return removeByIds(ids);
    }
}
